package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import fact.view.DTO.RoleDTO;
import fact.view.model.RoleModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;

@ManagedBean(name = "roleBean")
@RequestScoped
public class RoleBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	private SelectItem[] roleSelectList;
	
	@ManagedProperty(value="#{roleModelBean}")
	private RoleModelBean roleModelBean;
	
	ArrayList<RoleDTO> roleList=new  ArrayList<RoleDTO>();

	private RoleDTO role=new RoleDTO();
	private RoleDTO editRole=new RoleDTO();
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addRole(ActionEvent event) throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.role.getRoleName()==null || this.role.getRoleName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:roleName","Role Name cannot be empty.");
		}
		if(roleModelBean.getRole(this.role.getRoleName().trim())!=null)
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:roleName","Role Name Already Exist.");
		}
		
		if(opened==false)
		{
			if(roleModelBean.addRole(this.role.getRoleName())==true)
			{
				roleList=roleModelBean.getRoleList();
				session.setAttribute("session_roleList", roleList);
				
				FacesUtils.addInfoMessage(null,"Role has successfully been added.");

			}
			return "admin/role.jsf";
		}
		return null;
	}
	
	public String deleteRole() throws SQLException
	{
		System.out.println("Role Id :"+this.role.getRoleId());
		if(roleModelBean.deleteRole(this.role.getRoleId())==true)
		{
			roleList=roleModelBean.getRoleList();
			session.setAttribute("session_roleList", roleList);
			
			FacesUtils.addInfoMessage(null,"Role has successfully been deleted.");
		}
		return "admin/role.jsf";
	}
	
	 
	public String editRole() throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.editRole.getRoleName()==null || this.editRole.getRoleName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editRoleName","Role Name cannot be empty.");
		}
		
		if(editOpened==false)
		{
			System.out.println("Role Id :"+this.editRole.getRoleId());
			if(roleModelBean.editRole(this.editRole.getRoleName(),this.editRole.getRoleId())==true)
			{
				roleList=roleModelBean.getRoleList();
				session.setAttribute("session_roleList", roleList);
				
				FacesUtils.addInfoMessage(null,"Role has successfully been edited.");
			}
			return "admin/role.jsf";
			
		}
		return null;
		
	}

	public RoleModelBean getRoleModelBean() {
		return roleModelBean;
	}

	public void setRoleModelBean(RoleModelBean roleModelBean) {
		this.roleModelBean = roleModelBean;
	}

	public ArrayList<RoleDTO> getRoleList() {
		try{
			
			if(session.getAttribute("session_roleList")==null)
			{
				roleList=roleModelBean.getRoleList();
				session.setAttribute("session_roleList", roleList);
			}
			else
				roleList=(ArrayList<RoleDTO>)session.getAttribute("session_roleList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return roleList;
	}
	
	
	public SelectItem[] getRoleSelectList() {
		
		try{
			
			if(session.getAttribute("session_roleList")==null)
			{
				roleList=roleModelBean.getRoleList();
				session.setAttribute("session_roleList", roleList);
			}
			else
				roleList=(ArrayList<RoleDTO>)session.getAttribute("session_roleList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		 roleSelectList = new SelectItem[roleList.size()];
		 for(int i=0;i<roleList.size();i++)
			 roleSelectList[i] = new SelectItem(roleList.get(i).getRoleId(), roleList.get(i).getRoleName());
		 
		return roleSelectList;
	}

	public void setRoleSelectList(SelectItem[] roleSelectList) {
		this.roleSelectList = roleSelectList;
	}

	public void setRoleList(ArrayList<RoleDTO> roleList) {
		this.roleList = roleList;
	}

	

	public RoleDTO getRole() {
		return role;
	}

	public void setRole(RoleDTO role) {
		this.role = role;
	}

	public RoleDTO getEditRole() {
		return editRole;
	}

	public void setEditRole(RoleDTO editRole) {
		this.editRole = editRole;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.role=null;
	        opened = true;
	        editOpened = false;
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editRole=roleModelBean.getRole(this.editRole.getRoleId());
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}
